using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using CubeWars___Demolition.Services;
using CubeWars___Demolition.PhysicObjects;


namespace CubeWars___Demolition.GamePlay
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class shootingObjects : Microsoft.Xna.Framework.GameComponent
    {
        float power;
        bool charging, shoot;
        Camera camera;
        gameState gameState;
        Model box, sphere, capsule, cylinder;

        public float Power { get { return this.power; } set { } }
        public bool Charging { get { return this.charging; } set { this.charging = value; } }
        public bool Shoot { get { return this.shoot; } set { this.shoot = value; } }

        public shootingObjects(Game game)
            : base(game)
        {
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            box = Game.Content.Load<Model>("Models/box");
            sphere = Game.Content.Load<Model>("Models/sphere");
            capsule = Game.Content.Load<Model>("Models/capsule");
            cylinder = Game.Content.Load<Model>("Models/cylinder");

            // TODO: Add your initialization code here
            camera = Game.Services.GetService(typeof(Camera)) as Camera;
            gameState = Game.Services.GetService(typeof(gameState)) as gameState;
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (charging)
            {
                if (power <= 100.0f)
                    power += (float)gameTime.ElapsedGameTime.Milliseconds / 10;
            }
            if (shoot)
            {
                charging = false;
                shoot = false;
                shootObject();
                power = 0;
            }
            base.Update(gameTime);
        }

        private void shootObject()
        {
            switch (gameState.objectType)
            {
                //Caso 0: sfera
                case 0:
                    {
                        var sphereObject = new SphereObject(Game, sphere, gameState.ratius, Matrix.Identity, camera.Position + camera.ViewRotation.Forward * 20);
                        sphereObject.PhysicsBody.Velocity = camera.ViewRotation.Forward * power;
                        sphereObject.PhysicsBody.ApplyGravity = gameState.gravity;
                        Game.Components.Add(sphereObject);
                        sphereObject.DrawOrder = 1;
                        break;
                    }
                case 1:
                    {
                        var boxObject = new BoxObject(Game, box, gameState.boxSides, camera.ViewRotation, camera.Position + camera.ViewRotation.Forward * 2);
                        boxObject.PhysicsBody.Velocity = camera.ViewRotation.Forward * power;
                        Game.Components.Add(boxObject);
                        break;
                    }
                case 2:
                    {
                        var capsuleObject = new CapsuleObject(Game, capsule, gameState.ratius, gameState.lenght, camera.ViewRotation, camera.Position + camera.ViewRotation.Forward * 20);
                        capsuleObject.PhysicsBody.Velocity = camera.ViewRotation.Forward * power;
                        Game.Components.Add(capsuleObject);
                        break;
                    }
                case 3:
                    {
                        var cylinderObject = new CylinderObject(Game, gameState.ratius, gameState.lenght, camera.Position + camera.ViewRotation.Forward * 20, cylinder);
                        cylinderObject.PhysicsBody.Velocity = camera.ViewRotation.Forward * power;
                        Game.Components.Add(cylinderObject);
                        break;
                    }
            }
        }

    }
}
